﻿//*********************************************************
//做网站的时候 为了文件名到数据库好管理而做
//重复劳动的时候 尽量组织字符串
//文件 数据库 字符串 综合
//*********************************************************

using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.comboBox1.SelectedIndex = 0;
    }

    protected string GetConnString(string type)
    {
        string source = "";

        if (type.Equals("access"))
        {
            source = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + this.textBox4.Text;
        }
        if (type.Equals("sql"))
        {
            source = "server=(local); Integrated Security=SSPI; Database=" +
                this.textBox4.Text +
                  "; connection reset=false;" +
                  "connection lifetime=5;" +
                  "min pool size=5;" +
                  "max pool size=10";
        }

        return source;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.folderBrowserDialog1.ShowDialog();
        this.textBox1.Text = folderBrowserDialog1.SelectedPath;

        //this.openFileDialog1.ShowDialog();
        //this.textBox.Text = openFileDialog1.FileName;
    }

    private string[] FindAllFile(string path1)
    {
        FileInfo[] NewFileInfo;//当前所有文件
        DirectoryInfo FatherDirectory = new DirectoryInfo(path1); //当前目录
        NewFileInfo = FatherDirectory.GetFiles();//得到文件集，可以进行操作
        string[] temp = new string[NewFileInfo.Length];
        for (int i = 0; i < NewFileInfo.Length; i++)
        {
            string path = NewFileInfo[i].Name;
            temp[i] = path;
        }
        return temp;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string type = this.comboBox1.SelectedItem.ToString();

        string table = this.textBox2.Text;
        string column = this.textBox3.Text;
        string catalog = this.textBox1.Text;
        string sql;

        if (type.Equals("access"))
        {
            string[] values = this.FindAllFile(catalog);
            OleDbConnection con = new OleDbConnection(GetConnString(type));
            OleDbCommand com = new OleDbCommand();
            con.Open();
            com.Connection = con;
            for (int i = 0; i < values.Length; i++)
            {
                sql = "insert into " + table + "(" + column + ") values('" + values[i] + "')";
                com.CommandText = sql;
                com.ExecuteNonQuery();
            }
            com.Dispose();
            con.Close();
            MessageBox.Show("插入成功");
        }
        if (type.Equals("sql"))
        {
            string[] values = this.FindAllFile(catalog);
            SqlConnection con = new SqlConnection(GetConnString(type));
            SqlCommand com = new SqlCommand();
            con.Open();
            com.Connection = con;
            for (int i = 0; i < values.Length; i++)
            {
                sql = "insert into " + table + "(" + column + ") values('" + values[i] + "')";
                com.CommandText = sql;
                com.ExecuteNonQuery();
            }
            com.Dispose();
            con.Close();
            MessageBox.Show("插入成功");
        }
    }

    private void button4_Click(object sender, EventArgs e)
    {
        this.openFileDialog1.ShowDialog();
        textBox4.Text = openFileDialog1.FileName;
    }

    private void button3_Click(object sender, EventArgs e)
    {
        string type = this.comboBox1.SelectedItem.ToString();

        string table = this.textBox2.Text;

        string sql;

        if (type.Equals("access"))
        {
            OleDbConnection con = new OleDbConnection(GetConnString(type));
            OleDbCommand com = new OleDbCommand();
            con.Open();
            com.Connection = con;

            sql = "select * from " + table;
            com.CommandText = sql;

            OleDbDataAdapter ada = new OleDbDataAdapter();
            ada.SelectCommand = com;
            DataSet ds = new DataSet();
            ada.Fill(ds);
            DataTable dt = new DataTable();
            dt = ds.Tables[0];

            this.dataGridView1.DataSource = dt;
        }
        if (type.Equals("sql"))
        {
            SqlConnection con = new SqlConnection(GetConnString(type));
            SqlCommand com = new SqlCommand();

            com.Connection = con;

            sql = "select * from " + table;
            com.CommandText = sql;
            SqlDataAdapter ada = new SqlDataAdapter();
            ada.SelectCommand = com;
            DataSet ds = new DataSet();
            ada.Fill(ds);
            DataTable dt = new DataTable();
            dt = ds.Tables[0];

            this.dataGridView1.DataSource = dt;
        }
    }
}